package com.example.carbon.server.info;

import com.alibaba.fastjson2.JSON;
import com.example.carbon.Dto.energy.Search;
import com.example.carbon.dao.EnergyEfficiencyDao;
import com.example.carbon.pojo.energy.EnergyEfficiency;
import com.example.carbon.server.EnergyEfficiencyServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class EnergyEfficiencyInfo implements EnergyEfficiencyServer {
    @Resource
    EnergyEfficiencyDao energyEfficiencyDao;

    @Override
    public String getAllEnergyEfficiencyInfo() {
        try {
            List<EnergyEfficiency> energyEfficiencyList=energyEfficiencyDao.selectAllInfo();
            return JSON.toJSONString(energyEfficiencyList);
        }catch (Exception e){
            log.info(e.getMessage());
            return null;
        }
    }

    @Override
    public boolean deleteEnergyEfficiency(int id) {
        if (id==0){
            log.info("id不可以为零");
            return false;
        }
        try {
            if (energyEfficiencyDao.deleteEnergyInfo(id)){
                return true;
            }
            return false;
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean updateEnergyInfo(EnergyEfficiency energyEfficiency) {
        try {
            energyEfficiency.setUpdate_time(new Date());
            if (energyEfficiencyDao.updateEnergyInfo(energyEfficiency)){
                return true;
            }
            return false;
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public String searchKey(Search search) {
        try {
            return JSON.toJSONString(energyEfficiencyDao.searchKeyPoint(search));
        }catch (Exception e){
            log.info(e.getMessage());
            return null;
        }
    }

    @Override
    public boolean insertNewEnergyEfficiency(EnergyEfficiency energyEfficiency) {
        try {
            energyEfficiency.setUpdate_time(new Date());
            if (energyEfficiencyDao.insertNewEnergyInfo(energyEfficiency)){
                return true;
            }
            return false;
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }
}
